<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: UserService.cfc 875 2006-07-07 04:30:37Z mpwoodward $

Notes:
--->
<cfcomponent displayname="UserService" output="false" hint="UserService CFC for MachBlog">
	<cfproperty name="userGateway" type="UserGateway" required="true" />
	<cfproperty name="userDAO" type="UserDAO" required="true" />
	<cfproperty name="userFacade" type="org.machblog.facades.UserFacade" required="true" />
	<cfproperty name="passwordService" type="org.machblog.security.PasswordService" required="true" />
	
	<cffunction name="init" access="public" output="false" returntype="UserService" 
			hint="Constructor for this CFC">
		<cfreturn this />
	</cffunction>
	
	<cffunction name="setUserGateway" access="public" output="false" returntype="void">
		<cfargument name="userGateway" type="UserGateway" required="true" />
		<cfset variables.userGateway = arguments.userGateway />
	</cffunction>
	<cffunction name="getUserGateway" access="public" output="false" returntype="UserGateway">
		<cfreturn variables.userGateway />
	</cffunction>
	
	<cffunction name="setUserDAO" access="public" output="false" returntype="void">
		<cfargument name="userDAO" type="UserDAO" required="true" />
		<cfset variables.userDAO = arguments.userDAO />
	</cffunction>
	<cffunction name="getUserDAO" access="public" output="false" returntype="UserDAO">
		<cfreturn variables.userDAO />
	</cffunction>

	<cffunction name="setUserFacade" access="public" output="false" returntype="void">
		<cfargument name="userFacade" type="org.machblog.facades.UserFacade" 
				required="true" />
		<cfset variables.userFacade = arguments.userFacade />
	</cffunction>
	<cffunction name="getUserFacade" access="public" output="false" 
			returntype="org.machblog.facades.UserFacade">
		<cfreturn variables.userFacade />
	</cffunction>
	
	<cffunction name="setPasswordService" access="public" returntype="void" output="false"
		hint="Dependency: injected">
		<cfargument name="passwordService" type="org.machblog.security.PasswordService" required="true" />
		<cfset variables.passwordService = arguments.passwordService />
	</cffunction>	
	<cffunction name="getPasswordService" access="private" returntype="org.machblog.security.PasswordService" output="false">
		<cfreturn variables.passwordService />
	</cffunction>
	
	<cffunction name="getUserBean" access="public" output="false" returntype="User">
		<cfreturn createObject("component", "User").init() />
	</cffunction>
	
	<!--- gateway-related methods --->
	<cffunction name="getActiveUsers" access="public" output="false" returntype="query" 
			hint="Returns a query containing user account information for active accounts">
		<cfreturn variables.userGateway.getUsers(true) />
	</cffunction>
	
	<cffunction name="getAllUsers" access="public" output="false" returntype="query" 
			hint="Returns a query containing all user account information">
		<cfreturn variables.userGateway.getUsers(false) />
	</cffunction>
	
	<cffunction name="emailExists" access="public" output="false" returntype="boolean" 
			hint="Checks to see if an email address already exists for a user account in the system">
		<cfargument name="email" type="string" required="true" />
		
		<cfreturn getUserGateway().emailExists(arguments.email) />
	</cffunction>
	
	<!--- DAO-related methods --->
	<cffunction name="createUser" access="public" output="false" returntype="void" hint="Creates a user">
		<cfargument name="user" type="User" required="true" />
		
		<!--- hash the password before storing it in the database --->
		<cfset user.setPasswordSalt(createUUID()) />
		<cfset user.setPassword(getPasswordService().hashPassword(user.getPassword() & user.getPasswordSalt())) />
		
		<cfset getUserDAO().create(arguments.user) />
	</cffunction>
	
	<cffunction name="getUserById" access="public" output="false" returntype="User" 
			hint="Retrieves a user bean based on the ID passed in">
		<cfargument name="userId" type="string" required="true" />
		<cfset var user = getUserBean().init(arguments.userId) />
		<cfset getUserDAO().read(user) />
		<cfreturn user />
	</cffunction>
	
	<cffunction name="getUser" access="public" output="false" returntype="void">
		<cfargument name="user" type="User" required="true" />
		<cfset getUserDAO().read(arguments.user) />
	</cffunction>
	
	<cffunction name="updateUser" access="public" output="false" returntype="void" hint="Updates a user">
		<cfargument name="user" type="User" required="true" />
		
		<!--- if they're updating the password, hash the password before storing it in the database --->
		<cfif user.getPassword() NEQ "">
			<cfset user.setPassword(getPasswordService().saltAndHashPassword(user.getEmail(), user.getPassword())) />
		</cfif>
		
		<cfset getUserDAO().update(arguments.user) />
	</cffunction>
	
	<cffunction name="deleteUser" access="public" output="false" returntype="void" hint="Deletes a user">
		<cfargument name="user" type="User" required="true" />
		<cfset getUserDAO().delete(arguments.user) />
	</cffunction>
</cfcomponent>
